package org.example.servlet;

import org.example.expection.AppException;
import org.example.model.JSONResponse;
import org.example.util.JSONUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public abstract class AbstractBaseServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //设置请求体的编码格式
        req.setCharacterEncoding("UTF-8");
        //设置响应体的编码
        resp.setCharacterEncoding("UTF-8");
        //设置响应体的数据类型（浏览器采取什么方式执行）
        resp.setContentType("application/json");
        //Session会话管理：除登录和注册接口，其他都需要登录后访问
        //req.getServletPath()获取请求服务路径
        //TODO

        JSONResponse json=new JSONResponse();//统一返回给前端的格式
        try {
            //调用子类重写的方法
            Object data=process(req, resp);//login servlet中重写了这个方法
            //子类的process执行完没有抛异常，表示业务执行成功
            json.setSuccess(true);
            json.setData(data);

        }catch (Exception e){
            //异常如何处理？JDBC的异常SQLException
            e.printStackTrace();
            //json.setSuccess(false)不用设置，因为new的时候就是
            String code="UNKNOWN";
            String s="未知的错误";
            if(e instanceof AppException){
                code=((AppException) e).getCode();
                s=e.getMessage();
            }
            json.setCode(code);
            json.setMessage(s);
        }
        PrintWriter pw=resp.getWriter();
        pw.println(JSONUtil.serialize(json));//把json序列化后打印
        pw.flush();
        pw.close();
    }
    protected abstract Object process(HttpServletRequest req,
                                    HttpServletResponse resp) throws Exception;
}
